package com.example.demo.controller;



import com.example.demo.dao.ProductDao;
import com.example.demo.model.Manager;
import com.example.demo.model.Product;
import com.example.demo.service.ProductService;
import com.example.demo.service.impl.CartServiceimpl;
import com.example.demo.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class ProductController extends BaseController{
    @Resource
    ProductDao productDao;
    @Resource
    ProductService productService;

    @GetMapping("/myproducts")
    public JsonResult<List<Product>> MyProducts(HttpSession session) {
        List<Product> products=productDao.findByUid(getUidFromSession(session));
        return new JsonResult<>(OK,products);
    }

    @PostMapping("{pid}/updateProduct")
    public JsonResult<Void> updateProduct(@PathVariable("pid")Integer pid, Model model){

    /*    Product product= productService.getById(pid);
        model.addAttribute("product",product);*/
        return new JsonResult<>(OK);

    }

    @PostMapping("/saveProduct")
    public JsonResult<Void> saveProduct(Integer pid,Product product){
        Product newProduct=productDao.findById(pid);
        newProduct.setTypes(product.getTypes());
        newProduct.setPname(product.getPname());
        newProduct.setPrice(product.getPrice());
        newProduct.setStock(product.getStock());
        newProduct.setImg(product.getImg());
        productService.updateById(newProduct);
        return new JsonResult<>(OK);
    }

    @PostMapping("{pid}/deleteProduct")
    public JsonResult<Void> deleteProduct(@PathVariable("pid")Integer pid) {
        productDao.deleteProduct(pid);
        return new JsonResult<>(OK);
    }

    @PostMapping("/typedaliy")
    public JsonResult<List<Product>> showDaily() {
        List<Product> products=productDao.findByTypes("日常");
        return new JsonResult<>(OK,products);
    }


}
